package org.nobject.common.fan.datagen;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.nobject.common.lang.StringUtils;

/**
 * 身份证生成器
 *
 * @author benson
 * @version 1.0
 */
public class IDGen implements RandomGen{

	/**
	 * 校验ID18
	 * 
	 * @param id18
	 * @return
	 */
	public static boolean validateID18(String id18) {
		int tmp = 0;
		for (int i = 0; i < id18.length() - 1; i++) {
			tmp += Integer.valueOf(id18.charAt(i) + "") * vx[i];
		}
		if (vy[tmp % 11].equals((id18.charAt(id18.length() - 1)) + "")) {
			return true;
		}
		return false;

	}

	/**
	 * 生成ID18
	 * 
	 * @param sex
	 * @param area
	 * @param birthday
	 * @return
	 */
	public static String genID18(String sex, String area, String birthday) {

		// 性别:可以指定性别，或者输入NULL自动选择性别
		boolean male = false;
		if (sex == null)
			male = random.nextBoolean();
		else {
			if (sex.equalsIgnoreCase("male") || sex.equalsIgnoreCase("m") || sex.indexOf("男") >= 0 || sex.equalsIgnoreCase("Y") || sex.equalsIgnoreCase("true")) {
				male = true;
			}
		}

		// 地区:可以指定地区，或者大范围的地区，或者随机选择地区
		String areaNum = null;
		if (!StringUtils.isEmpty(area)) {
			area = area.trim();
			if (area.matches("\\d+")) {
				List tmpAreas = new ArrayList();
				boolean exsit = false;
				if (area.length() == 6) {
					for (int i = 0; i < DistrictGen.areaCodes.length; i++) {
						if (DistrictGen.areaCodes[i].equals(area)) {
							exsit = true;
							break;
						}
					}
					if (exsit)
						areaNum = area;
					else
						areaNum = DistrictGen.areaCodes[random.nextInt(DistrictGen.areaCodes.length)];
				} else {
					for (int i = 0; i < DistrictGen.areaCodes.length; i++)
						if (DistrictGen.areaCodes[i].startsWith(area))
							tmpAreas.add(DistrictGen.areaCodes[i]);
					if (tmpAreas.size() == 0)
						areaNum = DistrictGen.areaCodes[random.nextInt(DistrictGen.areaCodes.length)];
					else
						areaNum = (String) tmpAreas.get(random.nextInt(tmpAreas.size()));
				}
			} else {
				System.out.println("[WARN]地区格式异常:" + area);
				areaNum = DistrictGen.areaCodes[random.nextInt(DistrictGen.areaCodes.length)];
			}
		} else {
			areaNum = DistrictGen.areaCodes[random.nextInt(DistrictGen.areaCodes.length)];
		}

		// 生日:可以指定生日，或年份，或者随机（当年-1900）
		String birthdayNum = null;
		int maxYear = Calendar.getInstance().get(Calendar.YEAR);
		int minYear = 1900;
		int between = maxYear - minYear;

		int year = -1;
		int month = -1;
		int day = -1;

		if (!StringUtils.isEmpty(birthday)) {
			birthday = birthday.trim().replaceAll("-", "");
			if (birthday.matches("\\d+")) {
				
				if(birthday.length()<4){
					birthday+=random.nextInt(Integer.valueOf((1+StringUtils.getRepeatStrs("0",(4-birthday.length())))));
				}
				
				if (birthday.length() >= 4) {
					year = Integer.valueOf(birthday.substring(0, 4));
					if (year > maxYear || year < minYear) {
						System.out.println("[WARN]出生年份超出正常值范围:" + birthday);
						year = minYear + random.nextInt(between);
					}

					if (birthday.length() >= 6) {
						month = Integer.valueOf(birthday.substring(4, 6));
						if (month < 1 || month > 12) {
							System.out.println("[WARN]月份超出正常值范围:" + birthday);
							month = 1 + random.nextInt(12);
						}

						if (birthday.length() >= 8) {
							day = Integer.valueOf(birthday.substring(6, 8));
							
							if (day < 1 || day > 31) {
								System.err.println("day:"+day);
								System.out.println("[WARN]日超出正常值范围:" + birthday);
								day = 1 + random.nextInt(31);
							}
						}
					}
				}

				if (year == -1)
					year = minYear + random.nextInt(between);
				if (month == -1)
					month = 1 + random.nextInt(12);
				if (day == -1)
					day = 1 + random.nextInt(31);

			} else {
				System.out.println("[WARN]出生日期格式异常:" + area);
				year = minYear + random.nextInt(between);
				month = 1 + random.nextInt(12);
				day = 1 + random.nextInt(31);
			}
		} else {
			year = minYear + random.nextInt(between);
			month = 1 + random.nextInt(12);
			day = 1 + random.nextInt(31);
		}

		// 筛去不正确的日期 @todo

		birthdayNum = String.valueOf(year) + ((month < 10) ? ("0" + month) : month) + ((day < 10) ? ("0" + day) : day);

		// 出生序号
		String orderNum = null;

		int ion = random.nextInt(999);
		if ((male && (ion % 2 != 1)) || (!male && (ion % 2 != 0)))
			ion++;

		orderNum = "000" + ion;
		orderNum = orderNum.substring(orderNum.length() - 3, orderNum.length());

		// 校验码
		String validateNum = null;

		String str17 = areaNum + birthdayNum + orderNum;

		int tmp = 0;
		for (int i = 0; i < str17.length(); i++) {
			tmp += Integer.valueOf(String.valueOf(str17.charAt(i))) * vx[i];// 注意char转换
		}
		validateNum = vy[tmp % 11];

		return areaNum + birthdayNum + orderNum + validateNum;
	}

	// 第一位到第十七位的系数分别为
	public static int[] vx = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };

	// 余数对应字符
	public static String[] vy = { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" };

	
}
